package skyinline.swing.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLRecoverableException;

import javax.xml.transform.Result;

public class J26_SungJukDAO {

	private final String DRV = "oracle.jdbc.driver.OracleDriver";
	private final String URL = "jdbc:oracle:thin:skyinline/123456@112.216.253.125:1521:xe";
	private final String insertSQL = "insert into sungjuk values ( exam.nextval, ?, ?, ?, ?, ?, ?, ?, sysdate )";
	
	protected Connection openDatabase() {
		try {
			Class.forName(DRV);
			return DriverManager.getConnection(URL);
		} catch ( Exception e ) {
			e.printStackTrace();
		}
		return null;
	}
	
	protected void closeDatabase( Connection c, PreparedStatement p, ResultSet r ) {
		if ( r != null ) try { r.close(); r = null; } catch ( Exception e ) {}
		if ( p != null ) try { p.close(); p = null; } catch ( Exception e ) {}
		if ( c != null ) try { c.close(); c = null; } catch ( Exception e ) {}
	}
	
	//	성적 데이터 입력
	public boolean insertSungJuk( J26_SungJukVO sj ) {
		boolean result = false;
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try {
			conn = openDatabase();
			pstmt = conn.prepareStatement(insertSQL);
			pstmt.setString( 1, sj.getName());
			pstmt.setString( 2, sj.getKor());
			pstmt.setString( 3, sj.getEng());
			pstmt.setString( 4, sj.getMat());
			pstmt.setString( 5, sj.getTot());
			pstmt.setString( 6, sj.getAvg());
			pstmt.setString( 7, sj.getGrd());
			pstmt.executeUpdate();
			result = true;
		} catch ( SQLRecoverableException se ) {
			System.err.println("데이터베이스 연결에 실패했습니다. 네트워크를 확인하세요. 시스템을 종료합니다.");
			System.exit( 0 );
		}  catch (Exception ex ) {
			ex.printStackTrace();
		} finally {
			closeDatabase(conn, pstmt, null);
		}
		return result;
	}
}